package com.bysj.lms.controller;


import com.bysj.lms.common.ResultModel;
import com.bysj.lms.pojo.entity.CourseOutline;
import com.bysj.lms.pojo.entity.DictionaryItem;
import com.bysj.lms.pojo.entity.OutlineVersion;
import com.bysj.lms.service.ICourseOutlineService;
import com.bysj.lms.service.IDictionaryItemService;
import com.bysj.lms.service.IOutlineVersionService;
import com.bysj.lms.utils.EasyExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-02-04
 */
@RestController
@RequestMapping("/course-outline")
@Api(tags = "课程大纲管理")
public class CourseOutlineController {
    
    @Autowired
    ICourseOutlineService courseOutlineService;
    
    @Autowired
    IDictionaryItemService dictionaryItemService;
    @Autowired
    IOutlineVersionService outlineVersionService;
    
    @ApiOperation("得到课程大纲模型")
    @GetMapping()
    public CourseOutline getModel(){
        return new CourseOutline();
    }
    
//    @ApiOperation("导入大纲")
//    @PostMapping("/import")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "grade",value = "年级，例如2018级"),
//            @ApiImplicitParam(name = "majorCode",value = "专业代码")
//    })
//    public ResultModel importExcel(@RequestBody OutlineVersion outlineVersion,MultipartFile excel) throws IOException {
//
////        outlineVersionService
//
//        List<CourseOutline> read = EasyExcelUtil.read(excel, CourseOutline.class,2);
//        read.forEach(courseOutline -> System.out.println(courseOutline));
//        read.forEach(courseOutline -> {
////            courseOutline.setVersionNumber(versionNumber);
//        });
//        boolean adds = courseOutlineService.adds(read);
//        return adds?ResultModel.success():ResultModel.failure();
//    }
    
    @ApiOperation("课程大纲列表")
    @GetMapping("list")
    public ResultModel list(String versionNumber){
        List<CourseOutline> list = courseOutlineService.list(versionNumber);
        return ResultModel.success("查询成功",list);
    }
    
    @ApiOperation("修改")
    @PutMapping("/edit")
    public ResultModel edit(@RequestBody CourseOutline courseOutline){
        boolean update = courseOutlineService.updateById(courseOutline);
        return update?ResultModel.success():ResultModel.failure();
    }
    @ApiOperation("新增")
    @PutMapping("/add")
    public ResultModel add(@RequestBody CourseOutline courseOutline){
        boolean save = courseOutlineService.save(courseOutline);
        return save?ResultModel.success():ResultModel.failure();
    }
    @ApiOperation("删除")
    @PutMapping("/delete")
    public ResultModel delete(Integer id){
        boolean delete = courseOutlineService.removeById(id);
        return delete?ResultModel.success():ResultModel.failure();
    }
    
    @ApiOperation("课表大纲导出")
    @GetMapping("/export")
    public ResultModel export(String versionNumber, HttpServletResponse response) throws IOException {
        List<CourseOutline> list = courseOutlineService.list(versionNumber);
        if(list.size()==0){
            list.add(new CourseOutline());
        }
        OutlineVersion outlineVersion = outlineVersionService.getByNumber(versionNumber);
        DictionaryItem major = dictionaryItemService.find("专业", outlineVersion.getMajor());
        String filename = major.getName()+outlineVersion.getName()+"课程大纲";
        EasyExcelUtil.write(response,filename,"课程大纲",CourseOutline.class,list);
        return ResultModel.success("导出成功",filename+".xlsx");
    }
}

